package boyan.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * 
 * @author haipeng.liang <br>
 * file: com.zhibo.craw.biz.utils.HttpClient<br>
 * time: 2017年10月17日下午2:25:58<br>
 * 作用:
 */
public class HttpClient {
	
	
	/**
	 * 去除存在dom的文件，不存在dom的不要使用这个
	 * @param url
	 * @param Header
	 * @param charSet
	 * @return
	 */
	public static String HttpGetXML(String url,Map<String,String> Header,String charSet) {
		
		String randomIp = IpUtil.getRandomIp();
		Header.put("x-forwarded-for", randomIp);
		Header.put("Proxy-Client-IP", randomIp);
		Header.put("WL-Proxy-Client-IP", randomIp);
		
		StringBuilder sb=new StringBuilder();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        for (Entry<String, String> e : Header.entrySet()) {
        	get.setHeader(e.getKey(),e.getValue());
		}
        InputStream inStream = null;
        try {
            CloseableHttpResponse response = httpClient.execute(get);
            HttpEntity entity = response.getEntity();
            inStream= entity.getContent();
            
            byte[] cbuf = new byte[1024];
            int read = inStream.read(cbuf, 0, 3);
            if(read>3) {
            	if(cbuf[0]!=-17&&cbuf[0]!=-69&&cbuf[0]!=-65) {
            		sb.append(new String(cbuf).toString());
            	}
            }
            int read2 =0;
            String string  = null;
            while ((read2 = inStream.read(cbuf))> 0) {
            	string = new String(cbuf, 0, read2, charSet);
            	sb.append(string);
            }
            EntityUtils.consume(entity);
        } catch (Exception e) {
        	e.printStackTrace();
        }finally {
        	try {
        		if(inStream!=null) {
        			inStream.close();
        		}
        		httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
        return sb.toString();
	}
	
	/**
	 * 
	 * 作用 ： 
	 * @param url
	 * @param Header
	 * @param charSet
	 * @return
	 */
	public static String HttpGetIn(String url,Map<String,String> Header,String charSet) {
		
		String randomIp = IpUtil.getRandomIp();
		Header.put("x-forwarded-for", randomIp);
		Header.put("Proxy-Client-IP", randomIp);
		Header.put("WL-Proxy-Client-IP", randomIp);
		
		StringBuilder sb=new StringBuilder();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        
        HttpGet get = new HttpGet(url);
        
        
        for (Entry<String, String> e : Header.entrySet()) {
        	get.setHeader(e.getKey(),e.getValue());
		}
        
        InputStreamReader isr = null;
        try {
   
            HttpResponse response = httpClient.execute(get);
            
            HttpEntity entity = response.getEntity();
            isr=new InputStreamReader(entity.getContent(),charSet);
            char[] cbuf = new char[1024];
            int i=0;
            while ((i = isr.read(cbuf)) > 0) {
            	sb.append(new String(cbuf,0,i).toString());
            	cbuf = new char[1024];
            }
            EntityUtils.consume(entity);
        } catch (Exception e) {
        	e.printStackTrace();
        }finally {
        	try {
        		if(isr!=null) {
				isr.close();
        		}
        		httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
        return sb.toString();
	}
	
	public static String HttpGetRe(String url,Map<String,String> Header,String charSet) {
		
		String randomIp = IpUtil.getRandomIp();
		Header.put("x-forwarded-for", randomIp);
		Header.put("Proxy-Client-IP", randomIp);
		Header.put("WL-Proxy-Client-IP", randomIp);
		
		StringBuilder sb=new StringBuilder();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        for (Entry<String, String> e : Header.entrySet()) {
        	get.setHeader(e.getKey(),e.getValue());
		}
        InputStreamReader isr = null;
        BufferedReader br=null;
        try {
            CloseableHttpResponse response = httpClient.execute(get);
            HttpEntity entity = response.getEntity();
            isr=new InputStreamReader(entity.getContent(),charSet);
            br = new BufferedReader(isr);
            
            while(br.ready()){
            	sb.append(br.readLine());
            }
            EntityUtils.consume(entity);
        } catch (Exception e) {
        	e.printStackTrace();
        }finally {
        	try {
        		if(isr!=null) {
				isr.close();
        		}
        		if(br!=null) {
        			br.close();
        		}
        		httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
        return sb.toString();
	}
	
	public class Charset{
		public static final String US_ASCII = "US-ASCII";
		public static final String ISO_8859_1 = "ISO-8859-1";
		public static final String UTF_8 = "UTF-8";
		public static final String UTF_16BE = "UTF-16BE";
		public static final String UTF_16LE = "UTF-16LE";
		public static final String UTF_16 = "UTF-16";
	}
	/**
	 * charset :Charset.UTF_8
	 * 
	 * 作用 ： 
	 * @param url
	 * @param Header
	 * @param charSet
	 * @return
	 */
	public static String HttpPost(String url,Map<String,String> Header,String charset) {
		
		String randomIp = IpUtil.getRandomIp();
		Header.put("x-forwarded-for", randomIp);
		Header.put("Proxy-Client-IP", randomIp);
		Header.put("WL-Proxy-Client-IP", randomIp);
		
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        String result = null;
        try {

        	List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        	for (Entry<String, String> e : Header.entrySet()) {
        		formparams.add(new BasicNameValuePair(e.getKey(), e.getValue()));
			}
        	
            httpPost.setEntity(new UrlEncodedFormEntity(formparams, charset));
            
            CloseableHttpResponse response = httpClient.execute(httpPost);
            
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
            return result;
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return result;
	}
	
}
